package com.xdy.server.core2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: XDY.
 * @Email: 2280944114@qq.com
 * @CreateTime: 2023-03-13  18:51
 * @Description: Web上下文容器
 * @Version: 1.0
 */
public class WebContext {
    private Map<String,String> servletMap = new HashMap<>();
    private Map<String,String> mappingMap = new HashMap<>();

    public WebContext(List<ServletEntity> servletEntities, List<MappingEntity> mappingEntities) {
        //将servlet集合的转换成map映射
        for (ServletEntity servletEntity : servletEntities) {
            servletMap.put(servletEntity.getName(),servletEntity.getClazz());
        }
        //mapping集合转换成map映射
        for (MappingEntity mappingEntity : mappingEntities) {
            for (String pattern : mappingEntity.getPatterns()) {
                mappingMap.put(pattern,mappingEntity.getName());
            }
        }
    }

    /**
     * 通过url-pattern获取对应servlet的全类名
     * @param pattern
     * @return
     */
    public String getClazz(String pattern){
        return servletMap.get(mappingMap.get(pattern));
    }
}
